package Demo;

public class TicketRunnable implements Runnable{

    int ticket=8;
    @Override
    public void run() {

            for (int i = 0; i < 3; i++) {

                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


                synchronized (this) {
                    if (ticket > 0) {
                        System.out.println(Thread.currentThread().getName() + "==>第" + (ticket--) + "张票");
                    }
                }
            }


    }

    public static void main(String[] args) {
        TicketRunnable ticketRunnable=new TicketRunnable();
        Thread thread=new Thread(ticketRunnable);
        Thread thread1=new Thread(ticketRunnable);
        Thread thread2=new Thread(ticketRunnable);
        Thread thread3=new Thread(ticketRunnable);
        thread.start();
        thread1.start();
        thread2.start();
        thread3.start();

    }
}
